// 引入api
import { reqUserAddress, reqTradeInfo,reqSubmitOrder } from '@/api';

const state = {
    // 用户地址信息
    address: [],
    // 交易信息
    tradeInfo:{},
    // 订单号
    orderId:''
};
const mutations = {
    GETUSERADDRESS: (state, address) => {
        state.address = address;
    },
    GETTRADEINFO:(state, tradeInfo) => {
        state.tradeInfo = tradeInfo;
    },
    SUBMITORDER:(state, orderId) => {
        state.orderId = orderId;
    }
};
const actions = {
    // 获取用户信息
    getUserAddress: async ({ state, commit, dispatch }) => {
        let result = await reqUserAddress();
        if (result.code == 200) {
            commit("GETUSERADDRESS", result.data)
        }
    },
    // 获取交易也信息
    getTradeInfo:async({state, commit, dispatch})=>{
        let result = await reqTradeInfo();
        if (result.code== 200) {
            commit('GETTRADEINFO', result.data)
        }
    },
    // 提交订单
    submitOrder: async({commit, dispatch,state},{tradeNo,data})=>{
        let result=await reqSubmitOrder(tradeNo,data);
        if (result.code== 200) {
            commit("SUBMITORDER", result.data)
            return 'ok'
        }else{
            return Promise.reject();
        }
    }

};

const getters = {};

export default {
    state,
    mutations,
    actions,
    getters
}
